﻿
using UnityEngine;
using UnityEngine.UI;

namespace XFGameFramework.TweenSystem
{
    /// <summary>
    /// Graphic拓展
    /// </summary>
    public static class GraphicExtensions
    {
        /// <summary>
        /// 渐变颜色
        /// </summary>
        /// <param name="graphic">Graphic对象</param>
        /// <param name="target">目标颜色</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<Color> TweenColor(this Graphic graphic, Color target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false,float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(graphic, graphic.color, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenColor, delay,0,loopCount);
        }

        /// <summary>
        /// 渐变透明度
        /// </summary>
        /// <param name="graphic">Graphic对象</param>
        /// <param name="target">目标透明度</param>
        /// <param name="duration">持续时间</param>
        /// <param name="loop">循环模式</param>
        /// <param name="ease">缓动曲线</param>
        /// <param name="curve">自定义的缓动曲线</param>
        /// <param name="ingoreTimeScale">是否忽略时间缩放</param>
        /// <param name="delay">延迟</param>
        /// <param name="loopCount">循环次数</param>
        /// <returns></returns>
        public static TweenerCore<float> TweenFade(this Graphic graphic, float target, float duration, LoopMode loop = LoopMode.Once, Ease ease = Ease.Linear, AnimationCurve curve = null, bool ingoreTimeScale = false, float delay = 0, int loopCount = -1)
        { 
            return TweenManager.ComponentTween(graphic, graphic.color.a, target, duration, loop, ease, curve, ingoreTimeScale, OnTweenFade, delay, 0, loopCount);
        }


        internal static void OnTweenColor(TweenerCore<Color> tweener)
        {
            if (tweener.Component is Graphic)
            {
                Graphic graphic = tweener.Component as Graphic;
                graphic.color = tweener.Value;
            }
        }

        internal static void OnTweenFade(TweenerCore<float> tweener)
        {
            if (tweener.Component is Graphic)
            {
                Graphic graphic = tweener.Component as Graphic;
                graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, tweener.Value); 
            }
        }

    }

}

